package com.crm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.sun.org.apache.regexp.internal.recompile;

public class BaseDao {
	private String dataSource;

	public BaseDao(String dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * �����ӳ��л�ȡ���Ӷ���
	 * 
	 * @return
	 */
	private Connection getConnection() {
		try {
			// ��ʼ������
			Context ctx = new InitialContext();
			// ��ȡ����Դ
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/" + this.dataSource);
			// ��ȡ���Ӷ���
			Connection conn = ds.getConnection();
			return conn;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * �ر���Դ
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	private void close(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * ִ��insert into��update��delete from���
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public boolean update(String sql, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(sql);
			bindParamters(stmt, params);
			return stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * ��ѯ���,ִ��
	 * 
	 * @param sql
	 * @param rowMapper
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... params) {
		Connection conn = getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement(sql);
			bindParamters(stmt, params);
			rs = stmt.executeQuery();
			List<T> list = new ArrayList<T>();
			while (rs.next()) {
				T t = rowMapper.rowMapper(rs);
				list.add(t);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn, stmt, rs);
		}
		return null;
	}

	public <T> T queryOne(String sql, RowMapper<T> rowMapper, Object... params) {
		List<T> list = query(sql, rowMapper, params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * �󶨲�������
	 * 
	 * @param stmt
	 * @param params
	 */
	private void bindParamters(PreparedStatement stmt, Object... params) {
		if (params != null) {
			for (int i = 0; i < params.length; i++) {
				try {
					stmt.setObject(i + 1, params[i]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				;
			}
		}
	}
}
